const { QueryTypes, Op, where } = require("sequelize");
const seq = require("../db/seq");
const quziGroup = require("../model/quziGroup.model")
const User = require("../model/user.model");
class quziService {
    async queryQuziListService(requestObj) {
        if(requestObj&&requestObj.groupName) {
            requestObj.groupName = {
                [Op.like]: `%${requestObj.groupName}%`,
            }
        }
        const res = await quziGroup.findAll({where:requestObj});
        return res;
    }


    async queryTalkByIdService(id) {
        if(id!=-1) {
            const res = await seq.query("select * from `bl-talk` where groupId=:id",{
                type:QueryTypes.SELECT,
                replacements:{id},
            })
            for(let i=0;i<res.length;i++) {
                let userInfo = await User.findOne({where:{id:res[i].userId}});
                console.log(userInfo,"userInfo");
                const {id,username,nickname,avatar,intro} = userInfo.dataValues;
                res[i].userInfo = {id,username,nickname,avatar,intro};
            }
            return res;
        } else {
            const res = await seq.query("select * from `bl-talk` where groupId is null",{
                type:QueryTypes.SELECT,
            })
            for(let i=0;i<res.length;i++) {
                let userInfo = await User.findOne({where:{id:res[i].userId}});
                console.log(userInfo,"userInfo");
                const {id,username,nickname,avatar,intro} = userInfo.dataValues;
                res[i].userInfo = {id,username,nickname,avatar,intro};
            }
            return res;
        }
        
    }
}

module.exports = new quziService();